package com.itheima.health.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.Menu;
import com.itheima.health.service.MenuService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
菜单控制层
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    MenuService menuService;

    //查询所有菜单信息

    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Menu> menus = menuService.findAll();
            return new Result(true, "查询菜单信息成功", menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "查询菜单信息失败");
        }
    }

    //查询主菜单信息
    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        try {
            return menuService.findPage(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }
    //查询子菜单信息
    @RequestMapping("findPageSonMenu")
    public PageResult findPageSonMenu(@RequestBody QueryPageBean queryPageBean){
        try {
            return menuService.findPageSonMenu(queryPageBean);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(0L,null);
        }
    }

    //查询所有子菜单信息
    @RequestMapping("findAllSonMenu")
    public Result findAllSonMenu(){
        try {
            List<Menu> sonMenuList=menuService.findAllSonMenu();
            return new Result(true,"查询子菜单数据成功",sonMenuList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询子菜单数据失败");
        }
    }

    //添加主菜单
    @RequestMapping("add")
    public Result add(Integer[] sonIds,@RequestBody Menu menu){
        try {
            menuService.add(sonIds,menu);
            return new Result(true,"添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单失败");
        }
    }
    //添加子菜单
    @RequestMapping("addSonMenu")
    public Result addSonMenu(@RequestBody Menu menu){
        try {
            menuService.addSonMenu(menu);
            return new Result(true,"添加菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"添加菜单失败");
        }
    }
    //获取指定id 的菜单的数据
    @RequestMapping("findById")
    public Result findById(Integer id){
        try {
            Menu menu=menuService.findById(id);
            return new Result(true,"获取菜单的数据成功",menu);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取菜单的数据失败");
        }
    }

    //获取主菜单所关联的所有子菜单的id值
    @RequestMapping("findSonIdAssociationParentIds")
    public Result findSonIdAssociationParentIds(Integer id){
        try {
            List<Integer> sonIdsList = menuService.findSonIdAssociationParentIds(id);
            return new Result(true,"获取主菜单所关联的子菜单的id值成功",sonIdsList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"获取主菜单所关联的子菜单的id值失败");
        }

    }
    //编辑 主菜单
    @RequestMapping("edit")
    public Result editParentMenu(Integer[] sonIds,@RequestBody Menu menu){
        try {
            menuService.editParentMenu(sonIds,menu);
            return new Result(true,"编辑主菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑主菜单失败");
        }
    }

    //编辑子菜单
    @RequestMapping("editSonMenu")
    public Result editSonMenu(@RequestBody Menu menu){
        try {
            menuService.editSonMenu(menu);
            return new Result(true,"编辑子菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"编辑子菜单失败");
        }
    }

    //删除主菜单
    @RequestMapping("delById")
    public Result delById(Integer id){
        try {
            menuService.delById(id);
            return new Result(true,"删除菜单成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"删除菜单失败");
        }
    }
}
